package com.example.sensorapp;

import androidx.appcompat.app.AppCompatActivity;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

import org.w3c.dom.Text;

public class sensors extends AppCompatActivity implements SensorEventListener {

    private SensorManager mSensorManager;
    TextView Orientation;
    TextView Magnetic;
    TextView Temerature;
    TextView Light;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sensors);
        //获取界面组件
        Orientation = findViewById(R.id.Orientation);
        Magnetic = findViewById(R.id.Magnetic);
        Temerature = findViewById(R.id.Temerature);
        Light = findViewById(R.id.Light);
        //获取传感器服务
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    }
    @Override
    protected void onResume()	{
        super.onResume();
        // 为系统的传感器注册监听器
        mSensorManager.registerListener((SensorEventListener) this,mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION) , SensorManager.SENSOR_DELAY_GAME);
        mSensorManager.registerListener((SensorEventListener) this,mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD) , SensorManager.SENSOR_DELAY_GAME);
        mSensorManager.registerListener((SensorEventListener) this,mSensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE) , SensorManager.SENSOR_DELAY_GAME);
        mSensorManager.registerListener((SensorEventListener) this,mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT) , SensorManager.SENSOR_DELAY_GAME);
        super.onResume();
    }
    @Override
    protected void onPause(){
        // 取消注册
        mSensorManager.unregisterListener((SensorEventListener) this);
        super.onPause();
    }
    @Override
    protected void onStop(){
        // 取消注册
        mSensorManager.unregisterListener((SensorEventListener) this);
        super.onStop();
    }
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy){
    }
    @Override
    public void onSensorChanged (SensorEvent event){
        float[] values = event.values;
        //获取出发event的传感器类型
       int sensorType = event.sensor.getType();
       StringBuilder sb = null;
       //判断是哪个传感器发生改变
        switch(sensorType)
        {
            //方向传感器
            case Sensor.TYPE_ORIENTATION:
                sb = new StringBuilder();
                sb.append("绕x转过的角度：").append(values[0]).append("\n");
                sb.append("绕y转过的角度：").append(values[1]).append("\n");
                sb.append("绕z转过的角度：").append(values[2]).append("\n");
                Orientation.setText(sb);

            case Sensor.TYPE_MAGNETIC_FIELD:
                sb = new StringBuilder();
                sb.append("X方向的磁场强度为：").append(values[0]).append("\n");
                sb.append("Y方向的磁场强度为：").append(values[1]).append("\n");
                sb.append("Z方向的磁场强度为：").append(values[2]).append("\n");
                Magnetic.setText(sb);

            case Sensor.TYPE_AMBIENT_TEMPERATURE:
                sb = new StringBuilder();
                sb.append("当前温度为：").append(values[0]).append("\n");
                Temerature.setText(sb);

            case Sensor.TYPE_LIGHT:
                sb = new StringBuilder();
                sb.append("当前光的强度为：").append(values[0]).append("\n");
                Light.setText(sb);

        }
    }

}
